package com.honeywen.mymvc.leetcode;

import com.alibaba.druid.sql.visitor.functions.Char;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 205. 同构字符串
 * https://leetcode-cn.com/problems/isomorphic-strings/
 * @author wangwei
 * @date 2019/3/4
 */
public class IsIsomorphicTest {

    public static void main(String[] args) {

        System.out.println(isIsomorphic("egg", "add"));
        System.out.println(isIsomorphic("foo", "bar"));
        System.out.println(isIsomorphic("paper", "title"));

        System.out.println(isIsomorphic2("egg", "add"));
        System.out.println(isIsomorphic2("foo", "bar"));
        System.out.println(isIsomorphic2("paper", "title"));


    }

    public static boolean isIsomorphic2(String s, String t) {

        if (s.length() != t.length()) {
            return false;
        }

        int MAXCHAR = 256;
        char maps[] = new char[MAXCHAR];
        char mapt[] = new char[MAXCHAR];

        for (int i = 0; i < s.length(); i++) {
            if (maps[s.charAt(i)] == 0 && mapt[t.charAt(i)] == 0) {
                maps[s.charAt(i)] = t.charAt(i);
                mapt[t.charAt(i)] = s.charAt(i);
                continue;
            }

            if (maps[s.charAt(i)] == t.charAt(i) && mapt[t.charAt(i)] == s.charAt(i)) {
                continue;
            }
            return false;
        }

        return true;
    }



    public static boolean isIsomorphic(String s, String t) {

        if (s.length() != t.length()) {
            return false;
        }


        String r1 = convert(s);
        String r2 = convert(t);

        return Objects.equals(r1, r2);


    }

    private static String convert(String s) {

        int idx = 1;
        StringBuilder sb = new StringBuilder();
        sb.append(idx);



        for (int i = 1; i < s.length(); i++) {

            char ch1 = s.charAt(i);
            boolean flag = false;
            for (int j = 0; j < i; j++) {
                char ch2 = s.charAt(j);
                if (ch2 == ch1) {
                    flag = true;
                    sb.append(sb.charAt(j));

                    break;
                }

            }

            if (!flag) {
                sb.append(++idx);
            }

        }

        return sb.toString();
    }
}
